<template>
	<view class="custom-tabbar" :class="{ 'isIos':isIos, 'custom-tabbar-fixed': isFixed, 'custom-unlined': unlined, 'custom-backdrop__filter': backdropFilter }"
		:style="{ background: backgroundColor, zIndex: isFixed ? zIndex : 'auto' }">
		<block v-for="(item, index) in tabBar" :key="index">
			<view class="custom-tabbar-item" :class="{ 'custom-item-hump': item.hump,'home-active': item.name == 'home'}"
				:style="{ backgroundColor: item.hump && !backdropFilter ? backgroundColor : 'none' }"
				@tap="tabbarSwitch(index, item.hump, item.pagePath, item.verify)" @touchend="maskTouchend">
				<view class="custom-icon-box" :class="{ 'custom-tabbar-hump': item.hump }">
					<view class="unread" v-if="index == 2 && unread > 0">{{unread > 100 ? '99+' : unread}}</view>
					<image :src="current == index ? item.selectedIconPath : item.iconPath"
						:class="[item.hump ? '' : 'custom-tabbar-icon',{'custom-tabbar-icon-active': item.name == 'home'}]">
					</image>
					<view :class="[item.isDot ? 'custom-badge-dot' : 'custom-badge']" :style="{ color: badgeColor, backgroundColor: badgeBgColor }"
						v-if="item.num">
						{{ item.isDot ? '' : item.num }}
					</view>
				</view>
				<view v-if="item.name != 'home'" class="custom-text-scale" :class="{ 'custom-text-hump': item.hump }"
					:style="{ color: current == index ? selectedColor : color }">{{ item.text }}</view>
			</view>
		</block>
		<view :style="{ background: backgroundColor }" :class="{ 'custom-hump-box': hump }" v-if="hump && !unlined && !backdropFilter"></view>
	</view>
</template>

<script>
	import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
	import {
		mapState
	} from "vuex";
	const app = getApp();
	export default {
		name: 'tuiTabbar',
		mixins: [mixin],
		emits: ['click'],
		props: {
			//当前索引
			current: {
				type: Number,
				default: 0
			},
			//字体颜色
			color: {
				type: String,
				default: '#222222'
			},
			//字体选中颜色
			selectedColor: {
				type: String,
				default: '#222222'
			},
			//背景颜色
			backgroundColor: {
				type: String,
				default: '#FFFFFF'
			},
			//是否需要中间凸起按钮
			hump: {
				type: Boolean,
				default: false
			},
			//固定在底部
			isFixed: {
				type: Boolean,
				default: true
			},
			//tabbar
			// "pagePath": "/pages/my/my", 页面路径
			// "text": "thor", 标题
			// "iconPath": "thor_gray.png", 图标地址
			// "selectedIconPath": "thor_active.png", 选中图标地址
			// "hump": true, 是否为凸起图标
			// "num": 2,   角标数量
			// "isDot": true,  角标是否为圆点
			// "verify": true  是否验证  （如登录）
			tabBar: {
				type: Array,
				default () {
					return [];
				}
			},
			//角标字体颜色
			badgeColor: {
				type: String,
				default: '#fff'
			},
			//角标背景颜色
			badgeBgColor: {
				type: String,
				default: '#F74D54'
			},
			unlined: {
				type: Boolean,
				default: false
			},
			//是否开启高斯模糊效果[仅在支持的浏览器有效果]
			backdropFilter: {
				type: Boolean,
				default: false
			},
			//z-index
			zIndex: {
				type: [Number, String],
				default: 998
			}
		},
		data() {
			return {
				tabbarList: app.getTabBarLinks(),
				currentIndex: this.selectedIndex,
				isIos: null,
				touchNum: 0,
			};
		},
		computed: {
			...mapState({
				unread: state => state.unread,
				isSDKReady: state => state.isSDKReady,
			})
		},
		mounted() {
			if (this.isSDKReady) {
				this.getTotalUnreadMessage()
			}
		},
		watch: {
			isSDKReady(val) {
				if (val) {
					//会话未读总数
					this.getTotalUnreadMessage()
				}
			},
		},

		methods: {
			//会话未读总数
			getTotalUnreadMessage() {
				// let promise = uni.$TUIKit.getTotalUnreadMessageCount();
				// this.$store.commit("onTotalUnreadMessageCountUpdated", promise);
			},
			maskTouchend(e) {
				this.touchNum++
				setTimeout(() => {
					if (this.touchNum == 1) {
						console.log('单击')
					}
					if (this.touchNum >= 2) {
						this.$emit('dblclick');
					}
					this.touchNum = 0
				}, 250)
			},
			tabbarSwitch(index, hump, pagePath, verify) {
				if (index !== this.current) {
					uni.switchTab({
						url: `/${this.tabbarList[index]}`
					})
					// if (this.current == 2 || this.current == 3) {
					// 	uni.requestSubscribeMessage({
					// 		tmplIds: ['8fsA0Z1x6HQJqfrVFu53YoxjCKq5f-GCrPp2KIH_-NE',
					// 			'inhNL1YwOXhW1aayoGNTE7U0bsHm7mj04dfK8e7OGhQ',
					// 			'm6k8NIF9qd4-nIkl3ks5PlPOMvijInpBi0XzPmNbJvs'
					// 		],
					// 		success(res) {
					// 			console.log(res);
					// 		},
					// 		fail(err) {
					// 			console.log(err);
					// 		}

					// 	})
					// }
				}
				if (this.current != 0) {
					app.globalData.tabBar[0].name = 'active-home'
				} else {
					app.globalData.tabBar[0].name = 'home'
				}
				// if (index == 2) {
				// 	wx.requestSubscribeMessage({
				// 		tmplIds: ['inhNL1YwOXhW1aayoGNTE7U0bsHm7mj04dfK8e7OGhQ'],
				// 		success(res) {
				// 			console.log(res);
				// 		},
				// 		fail(err) {
				// 			console.log(err);
				// 		}
				// 	})
				// }

				this.$emit('click', {
					index: index,
					hump: hump,
					pagePath: pagePath,
					verify: verify
				});
			}

		}
	};
</script>

<style scoped>
	.custom-tabbar {
		width: 100%;
		height: 130rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: relative;
		box-shadow: 0px 0 19rpx 0px rgba(198, 204, 202, 0.56);
	}

	.isIos {
		height: 106rpx;
	}

	.isIos .custom-tabbar-item {
		padding: 14rpx 0 12rpx 0;
	}

	.custom-backdrop__filter {
		/* Safari for macOS & iOS */
		-webkit-backdrop-filter: blur(15px);
		/* Google Chrome */
		backdrop-filter: blur(15px);
	}

	.custom-tabbar-fixed {
		position: fixed;
		left: 0;
		bottom: 0;
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);
		box-sizing: content-box !important;
	}

	/* 	.custom-tabbar::before {
		content: ' ';
		width: 100%;
		border-top: 1px solid #b2b2b2;
		position: absolute;
		top: 0;
		left: 0;
		transform: scaleY(0.5) translateZ(0);
		transform-origin: 0 0;
		display: block;
		z-index: 3;
	} */

	.custom-tabbar-item {
		height: 100%;
		flex: 1;
		display: flex;
		text-align: center;
		align-items: center;
		flex-direction: column;
		justify-content: space-between;
		position: relative;
		padding: 25rpx 0;
		box-sizing: border-box;
		z-index: 5;
	}

	.custom-icon-box {
		position: relative;
		/* margin-bottom: 5rpx; */
	}

	.unread {
		position: absolute;
		top: -14rpx;
		right: -20rpx;
		z-index: 2;
		width: 28rpx;
		height: 28rpx;
		background: #FF4664;
		border: 4rpx solid #FFFFFF;
		border-radius: 50%;
		font-size: 24rpx;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 28rpx;
		text-align: center;
	}

	.custom-item-hump {
		height: 98rpx;
	}

	.home-active {
		justify-content: center;
	}

	.custom-tabbar-icon {
		width: 44rpx;
		height: 44rpx;
		display: block;
		/* transition: all .2s ease-out; */
	}

	.custom-tabbar-icon-active {
		width: 80rpx;
		height: 80rpx;
		display: block;
		transition: all .3s;
	}

	.custom-hump-box {
		width: 120rpx;
		height: 120rpx;
		position: absolute;
		left: 50%;
		transform: translateX(-50%);
		top: -50rpx;
		border-radius: 50%;
		z-index: 4;
	}

	.custom-hump-box::after {
		content: ' ';
		height: 200%;
		width: 200%;
		border: 1px solid #b2b2b2;
		position: absolute;
		top: 0;
		left: 0;
		transform: scale(0.5) translateZ(0);
		transform-origin: 0 0;
		border-radius: 120rpx;
		box-sizing: border-box;
		display: block;
	}

	.custom-unlined::after {
		height: 0 !important;
	}

	.custom-tabbar-hump {
		width: 100rpx;
		height: 100rpx;
		position: absolute;
		left: 50%;
		-webkit-transform: translateX(-50%) rotate(0deg);
		transform: translateX(-50%) rotate(0deg);
		top: -40rpx;
		-webkit-transition: all 0.2s linear;
		transition: all 0.2s linear;
		border-radius: 50%;
		z-index: 5;
	}

	/* 	.custom-tabbar-hump image {
		width: 100rpx;
		height: 100rpx;
		display: block;
	} */

	.custom-hump-active {
		-webkit-transform: translateX(-50%) rotate(135deg);
		transform: translateX(-50%) rotate(135deg);
	}

	.custom-text-scale {
		font-weight: bold;
		transform: scale(0.8);
		font-size: 25rpx;
		line-height: 28rpx;
		transform-origin: center 100%;
	}

	.custom-text-hump {
		position: absolute;
		left: 50%;
		bottom: 10rpx;
		transform: scale(0.8) translateX(-50%);
		transform-origin: 0 100%;
	}

	.custom-badge {
		position: absolute;
		font-size: 24rpx;
		height: 32rpx;
		min-width: 20rpx;
		padding: 0 6rpx;
		border-radius: 40rpx;
		right: 0;
		top: -5rpx;
		transform: translateX(70%);
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.custom-badge-dot {
		position: absolute;
		height: 16rpx;
		width: 16rpx;
		border-radius: 50%;
		right: -4rpx;
		top: -4rpx;
	}
</style>